import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<PayTeacherState> buildReducer() {
  return asReducer(
    <Object, Reducer<PayTeacherState>>{
      PayTeacherAction.purchase: _purchase,
      PayTeacherAction.button: _button,
    },
  );
}

PayTeacherState _purchase(PayTeacherState state, Action action) {
  PayTeacherState params = action.payload;
  // print('>>>> DD queryProductError ${params.queryProductError}');

  // print('>>>> DD products ----');
  params.products.forEach((product) {
    // print('>>>> DD product: ${product.toString()}');
  });

  // print('>>>> DD purchases ----');
  params.purchases.forEach((purchase) {
    // print('>>>> DD purchase: ${purchase.productID}');
  });
  // print('>>>> DD notFoundIds ${params.notFoundIds}');
  // print('>>>> DD consumables ${params.consumables}');
  // print('>>>> DD purchasePending ${params.purchasePending}');
  // print('>>>> DD loadingPurchase ${params.loadingPurchase}');
  return state.clone()
    ..isPurchaseAvailable = params.isPurchaseAvailable
    ..queryProductError = params.queryProductError
    ..products = params.products
    ..purchases = params.purchases
    ..notFoundIds = params.notFoundIds
    ..consumables = params.consumables
    ..purchasePending = params.purchasePending
    ..loadingPurchase = params.loadingPurchase;
}

PayTeacherState _button(PayTeacherState state, Action action) {
  return state.clone()..buttonText = action.payload;
}
